<?php
/**
 * 分页函数
 * 描述：分页函数,包括超长文本分页
 * $total--总记录数， $display = 每页显示数 ，$shownum=0,$showtext=0,$showselect=0 三种显示形式， $showsuo = 7 缩略的中间码， $url
 * @author		YZG (yangzhiguo0903@163.com)
 * @copyright	(c) 2010 by YZG
 */
@$page = $_GET['page'];
function pageft($total, $display = 20, $shownum = 0, $showtext = 0, $showselect = 0, $showsuo = 7, $url = '')
{
	//全局化部分变量
	global $page, $offset, $_SERVER, $pagenav;
	
	//处理当前页
	if(!$page) $page = 1;
	
	//处理url
	if(!$url)
	{	
		$url = $_SERVER['PHP_SELF'];
	}
	$url = parse_url($url);
	$url = $url['path'];
	
	//页码计算
	$lastpg = ceil($total/$display);	//最后一页
	$page   = min($page, $lastpg);		//保证页码不会超过$lastpg
	$prevpg = $page - 1;				//上一页
	$nextpg = ($page == $lastpg) ? 0 : $page + 1;	//下一页
	$offset = ($page - 1) * $display;	//数据查询开始位置
	
	//显示页码信息
	if($showtext == 1)
	{
		$pagenav = '显示' . ($total ? $offset+1 : 0) . '～' . min(($offset+$display), $total) . '条&nbsp;';
		$pagenav .= '共' . $total . '条记录，' . $page . '/' . $lastpg . '页';
	}
	else
	{
		$pagenav = '';
	}
	
	if($lastpg <=1) return false; //只有一页不显示上下页
	if($prevpg)
	{
		$pagenav .= '<a href="' . $url . '?page=1">首页</a>';
	}
	else
	{
		$pagenav .= '<span class="disabled">首页</span>';
	}	
	if($prevpg)
	{
		$pagenav .= '<a href="' . $url . '?page=' . $prevpg . '">上一页</a>';
	}
	else
	{
		$pagenav .= '<span class="disabled">上一页</span>';
	}
	
	//显示页码
	if($shownum == 1)	//显示页码
	{
		$mid   = ceil($showsuo/2);	//相当于当前页
		$start = $page - $mid;		//中间页码起始处
		
		//处理起始处
		if($start <= 0)
		{
			$start = 0;	
		}
		//第一页总是显示
		if($page == 1)
		{
			$pagenav .= '<span class="current">1</span>';
		}
		else
		{
			$pagenav .= '<a href="' . $url . '?page=1">1</a>';
		}
		
		//显示中间页码部分,显示showsuo个页码
		for($i = 1; $i <=$showsuo; $i++)	
		{
			//总页数为1时推出
			if($lastpg == 1)
			{
				break;
			}
			
			$yema = $start + $i;	//页码起始的实际页码数
			//跳过第一页码
			if($yema == 1)
			{
				continue;
			}
			
			//输出省略号，起始位置同时起始页码大于2
			if($i == 1 && $yema> 2)
			{
				$pagenav .= '...';
			}
			
			//最后一页跳出
			if($yema == $lastpg)
			{
				break;
			}
			//当前页输出页码
			if($yema == $page)
			{
				$pagenav .= "<span class='current'>{$yema}</span>";
			}
			else
			{
				$pagenav .= "<a href='{$url}?page={$yema}'>{$yema}</a>";
			}
			
			//结束位置的省略号
			if($showsuo == $i && $yema <=($lastpg - 1))
			{
				$pagenav .= '...';
			}
		}
		
		//最后一页总是显示
		if($page == $lastpg)
		{
			$pagenav .= '<span class="current">' . $lastpg . '</span>';
		}
		else
		{
			$pagenav .= "<a href='{$url}?page={$lastpg}'>{$lastpg}</a>";
		}	
	}
	
	
	
	//下一页
	if($nextpg)
	{
		$pagenav .= '<a href="' . $url . '?page=' . $nextpg . '">下一页</a>';
	}
	else
	{
		$pagenav .= '<span class="disabled">下一页</span>';
	}
	if($nextpg)
	{
		$pagenav .= '<a href="' . $url . '?page=' . $lastpg . '">尾页</a>';
	}
	else
	{
		$pagenav .= '<span class="disabled">尾页</span>';
	}
	
	//最后输出跳转
	if($showselect == 1)
	{
		$pagenav .= "跳转<select name='topage' size='1' onchange=\"window.location='{$url}?page='+this.value\">\n";
		for($i = 1; $i <=$lastpg; $i++)
		{
			if($page == $i)
			{
				$pagenav .= "<option value='{$i}' selected='selected'>{$i}</option>\n";
			}
			else
			{
				$pagenav .= "<option value='{$i}'>{$i}</option>";
			}
		}
		$pagenav .= '</select>';
	}
}

//header('Content-type:text/html; charset="utf-8"');
pageft(10000, 20, 1, 1, 1, 7);
//echo $pagenav;
?>